package com.hd.crdao.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.DomainNaneAdapter;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.DomainNameBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.RefreshListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/7/3 0003
 */

public class DomainNameSettingActivity extends BaseActivity implements RefreshListView.OnRefreshListener {
    @BindView(R.id.back)
    ImageButton mBack;
    @BindView(R.id.ll_search)
    LinearLayout mLlSearch;
    @BindView(R.id.cancel)
    TextView mCancel;
    @BindView(R.id.refreshview_domain)
    RefreshListView mRefreshviewDomain;
    public DomainNaneAdapter mDomainNaneAdapter;
    public Handler mHandler = new Handler();
    List<DomainNameBean.RowsBean> mList;
    public String mBusinessid;
    public int i =1;  //分页的页数
    public int mTotal;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_domain;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        mList =  new ArrayList<>();
    }

    @Override
    protected void initListener() {
        mRefreshviewDomain.setOnRefreshListener(this);
        mRefreshviewDomain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                position =position-1;
                DomainNameBean.RowsBean rowsBean = mList.get(position);
                String domainName = rowsBean.getDomainName();
                double domainPrice = rowsBean.getDomainPrice();
                String id1 = rowsBean.getId();
                Intent intent = new Intent(UIUtils.getContext(),BuyDomainActivity.class);
                intent.putExtra("domainName",domainName);
                intent.putExtra("domainPrice",domainPrice);
                intent.putExtra("id",id1);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        mBusinessid = SPUtils.getString("businessid", "");
        loadData(mBusinessid);
    }

    /**
     * 从服务器获取数据加载
     */
    private void loadData(String businessid) {

        try {
            OkHttpUtils.post().url(Constants.DOMAIN_NAME_LIST_URL).addParams("pageNo", "" + i).addParams("pageSize", "30")
                    .addParams("carBusinessId", businessid).addParams("sign", Constants.SIGN).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyToast.show("数据加载失败,请检查网络");
                }

                @Override
                public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        DomainNameBean domainNameBean = gson.fromJson(response, DomainNameBean.class);
                        String success = domainNameBean.getSuccess();       //请求成功的成功码
                        String message = domainNameBean.getMessage();
                    ALog.d("成功码success=" + success);
                        if(TextUtils.equals("00",success)){
                            // 总的数据条数  用于分页
                            mTotal = domainNameBean.getTotal();
                            List<DomainNameBean.RowsBean> rows = domainNameBean.getRows();
                            if(rows != null){
                                mList.addAll(rows);
                            }
                            ALog.d("mlist长度=" + mList.size());
                            mDomainNaneAdapter = new DomainNaneAdapter(mList);
                            mRefreshviewDomain.setAdapter(mDomainNaneAdapter);
                        }else {
                            MyToast.show(message);
                        }

                    }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @OnClick({R.id.back, R.id.ll_search, R.id.cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.ll_search:
                startActivity(new Intent(UIUtils.getContext(),SearchDomainActivity.class));
                break;
            case R.id.cancel:
                finish();
                break;
        }
    }

    @Override
    public void onDownPullRefresh() {
        i=1;

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadData(mBusinessid);
                mDomainNaneAdapter.notifyDataSetChanged();
                mRefreshviewDomain.hideHeaderView();
            }
        },2000);
    }

    @Override
    public void onLoadingMore() {
        i++;
        ALog.d("mTotal="+mTotal);
        if(mTotal/30 > i){
           mHandler.postDelayed(new Runnable() {
               @Override
               public void run() {
                   loadData(mBusinessid);
                   mDomainNaneAdapter.notifyDataSetChanged();
                   mRefreshviewDomain.hideFooterView();
               }
           },2000);
        }
    }
}
